﻿using System;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.Threading;

class UdpClientDemo
{
    const string SERVER_HOST = "127.0.0.1";
    const int SERVER_PORT = 1111;

    public static void Main()
    {
        Thread t = new Thread(StartListening);
        t.Start();

        string welcomeMsg = "Hello";
        byte[] data = Encoding.ASCII.GetBytes(welcomeMsg);
        UdpClient udpClient = new UdpClient(SERVER_HOST, SERVER_PORT);
        udpClient.Send(data, data.Length);
        Console.WriteLine("UDP packet sent to {0}:{1}.",
                          SERVER_HOST, SERVER_PORT);
    }

    private static void StartListening()
    {
        IPEndPoint senderSocket = new IPEndPoint(IPAddress.Parse(SERVER_HOST), SERVER_PORT);
        UdpClient udpClient = new UdpClient(SERVER_PORT);

        byte[] data = udpClient.Receive(ref senderSocket);
        string dataString = Encoding.ASCII.GetString(data);
       
        Console.WriteLine();
        Console.WriteLine("Recieved string: ");
        Console.WriteLine(dataString);
        Console.WriteLine();
    }
}
